package com.magina.machine.config.persister;

import com.magina.machine.model.OrderMachinaData.*;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 在内存中持久化状态机
 */
@Component
public class InMemoryStateMachinePersist implements StateMachinePersist<OrderStates, OrderEvents, String> {

	private static Map<String, StateMachineContext<OrderStates, OrderEvents>> map = new ConcurrentHashMap<>();
	
	@Override
	public void write(StateMachineContext<OrderStates, OrderEvents> context, String contextObj) throws Exception {
		map.put(contextObj, context);
	}

	@Override
	public StateMachineContext<OrderStates, OrderEvents> read(String contextObj) throws Exception {
		return map.get(contextObj);
	}

}